package com.navi.mould1.controller;
import com.navi.bean.table.Bis_user;
import com.navi.core.mysqlEntity.BisUserEntity;
import com.navi.core.repository.impl.MySqlRepositoryImpl;
import com.navi.core.util.JdbcUtils;
import com.navi.core.util.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;

import java.sql.Connection;
import java.util.List;

@Controller
public class LoginController {
    QueryRunner qr = new QueryRunner();

    @Autowired
    public MySqlRepositoryImpl mySqlRepositoryImpl;

    @Value("${session.reportUrl}")
    private String reportUrl;


    @RequestMapping("/")
    public ModelAndView login(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        request.getSession().removeAttribute("hasTryFlg");
        String userId = (String) request.getSession().getAttribute("userId");
        mv.addObject("reportUrl", reportUrl);
        if(userId == null){
            mv.setViewName("login-lte");
            return mv;
        }else{
            mv.setViewName("home");
            return mv;
        }
    }

    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response, String userid, String password) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("reportUrl", reportUrl);
        request.getSession().removeAttribute("hasTryFlg");
        String userId = (String) request.getSession().getAttribute("userId");
        if(userId != null){
            mv.setViewName("home");
            return mv;
        }
        if ( userid != null && password != null ){
            try (Connection conn = JdbcUtils.getMySQLConnection()){
                List<Bis_user> bis_user_list = qr.query(conn, "select * from icim_jn.bis_user where usr_id='"+userid+"' and usr_key='"+password+"'", new BeanListHandler<>(Bis_user.class));
                if (CollectionUtils.isNotEmpty(bis_user_list)) {
                    Bis_user user = bis_user_list.get(0);
                    HttpSession session = request.getSession();
                    session.setAttribute("hasTryFlg", "Y");
                    session.setAttribute("userId", user.getUsr_id());
                    session.setAttribute("userName", user.getUsr_name());
                    session.setAttribute("fabId", user.getFab_id());
                    session.setMaxInactiveInterval(10*60*60);//设置session过期时间，单位为秒，设置为-1永不过期
                    mv.setViewName("home");
                    return mv;
                }
            } catch (Exception e) {
                LoggerFactory.getLogger(this.getClass()).error(LoggerUtil.stackTraceToString(e));
                e.printStackTrace();
            }
        }
        mv.setViewName("login-lte");
        return mv;
    }

    @RequestMapping("/loginOut")
    public ModelAndView loginOut(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        request.getSession().removeAttribute("userName");
        request.getSession().removeAttribute("userId");
        request.getSession().removeAttribute("fabId");
        request.getSession().removeAttribute("hasTryFlg");
        mv.addObject("reportUrl", reportUrl);
        mv.setViewName("login-lte");
        return mv;
    }

    @Transactional
    @RequestMapping("/passwordUpdate")
    public ModelAndView passwordUpdate(String userid, String password) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("reportUrl", reportUrl);
        BisUserEntity bisUser = mySqlRepositoryImpl.get(BisUserEntity.class, userid);
        bisUser.setUsrKey(password);
        mySqlRepositoryImpl.update(bisUser);
        mv.setViewName("home");
        return mv;
    }
}
